package com.hxzy.shop.commodity.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.commodity.convert.DrugConvert;
import com.hxzy.shop.commodity.dto.req.*;
import com.hxzy.shop.commodity.dto.resp.*;
import com.hxzy.shop.commodity.feign.marketing.MarketingFeigh;
import com.hxzy.shop.commodity.mapper.*;
import com.hxzy.shop.commodity.service.*;
import com.hxzy.shop.entity.*;
import com.hxzy.shop.commodity.listener.ExcelBatchEditDrugListener;
import com.hxzy.shop.vo.ApiException;
import com.hxzy.shop.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.hxzy.shop.commodity.producer.DrugProducer;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug>  implements DrugService {
    @Resource
    private MarketingActivityTableMapper marketingActivityTableMapper;

    @Resource
    private DrugMapper drugMapper;

    @Resource
    private GroupBookingInnerDrugMapper groupBookingInnerDrugMapper;

    @Resource
    private MarketingFeigh marketingFeigh;

    @Resource
    private ClassifiedManagementMapper classifiedManagementMapper;

    @Resource
    private DrugInnerClassifiedManagementMapper drugInnerClassifiedManagementMapper;

    @Resource
    private DrugInnerClassifiedManagementService drugInnerClassifiedManagementService;

    @Resource
    private DrugCarouselMapService drugCarouselMapService;

    @Resource
    private DrugDetailsService drugDetailsService;

    @Resource
    private StoreMapper storeManager;
//    @Resource
//    private DrugProducer drugProducer;

    @Resource
    private DrugInnerStoreMapper drugInnerStoreMapper;
//    @Resource
//    private OriginalDrugMapper originalDrugMapper;
    @Resource
    private ClassifiedManagementService classifiedManagementService;
    @Resource
    private StoreImportRecordMapper storeImportRecordMapper;
    @Resource
    private DrugService drugService;

    @Resource
    private DrugProducer drugProducer;

//    @Override
//    public boolean addDrug(AddDrugReq addDrugReq) {
//        return false;
//    }

    /**
     * 更新状态
     *
     * @param state 更新状态
     * @return 是否成功
     */
    @Override
    public boolean updateState(DrugUpdateReq state) {
        //若药品参加了活动 只有活动结束了才可以对药品的状态进行修改
        Long drugId = state.getId();
        //药品参加了活动 并且活动未结束
        MarketingActivityTable marketing = marketingActivityTableMapper.selectOne(new LambdaQueryWrapper<MarketingActivityTable>()
                .eq(MarketingActivityTable::getDrugId, drugId)
                .eq(MarketingActivityTable::getActivitystatusEnd, true).last("limit 1")
        );
        //正在参加活动
        if (ObjectUtil.isNotEmpty(marketing)) {
            throw new ApiException(500, "不能对参加活动的药品进行修改");
        }
        //药品基本信息不完整 则不能上下架
        Drug d = drugMapper.selectOne(new LambdaQueryWrapper<Drug>().eq(Drug::getId, state.getId()));
        if (!checkDrug(d)) {
            throw new ApiException(500, "药品基本信息不完整，请先编辑录入完整的药品基本信息");
        }
        if (ObjectUtil.isNotEmpty(state.getState()) && !state.getState()) {
            //是否积分商品
//            Result<Boolean> pointsReward = marketingFeigh.getPointsReward(d.getId().toString(), "1", d.getMerchantId().toString());
//            if (pointsReward.getData()) {
//                throw new ApiException(500, "不能对参加活动的药品进行修改");
//            }
            //判断是否参加团购
            List<GroupBookingInnerDrug> groupBookingInnerDrugList = groupBookingInnerDrugMapper.getGroupBookingInnerDrug(drugId);

            if (ObjectUtil.isNotEmpty(groupBookingInnerDrugList)) {
                throw new ApiException(500, "不能对参加活动的药品进行修改");
            }
        }

        this.update(new LambdaUpdateWrapper<Drug>()
                .set(!ObjectUtil.isEmpty(state.getState()), Drug::getState, state.getState())
                .set(!ObjectUtil.isEmpty(state.getUniformRetailPrice()), Drug::getUniformRetailPrice, state.getUniformRetailPrice())
                .eq(Drug::getId, state.getId())
        );

        if (ObjectUtil.isNotEmpty(state.getState())) {
            //上下架
            Drug drug = drugMapper.selectById(state.getId());
//            drugProducer.sendDrugChangeMsg(new DrugChangeMessage().
//                    setDrugId(state.getId()).
//                    setChangeType(4).
//                    setState(state.getState()).setMerchantId(drug.getMerchantId()));
        } else {
            //改价格
            Drug drug = drugMapper.selectById(state.getId());
//            drugProducer.sendDrugChangeMsg(new DrugChangeMessage().
//                    setDrugId(state.getId()).
//                    setChangeType(2)
//                    .setMerchantId(drug.getMerchantId()));
        }
        return true;
    }

    /**
     * 子门店修改库存
     *
     * @param stock
     * @return boolean
     * @author qy
     * @date 2021/8/4
     * @description
     **/
    @Override
    public boolean updateStock(DrugUpdateReq stock) {
        DrugInnerStore drugInnerStore = drugInnerStoreMapper.selectOne(new LambdaQueryWrapper<DrugInnerStore>()
                .eq(DrugInnerStore::getDrugId, stock.getId())
                .eq(DrugInnerStore::getStoreId, stock.getStoreId()));
        if (!ObjectUtil.isEmpty(drugInnerStore)) {
            drugInnerStore.setStock(stock.getStock());
            int i = drugInnerStoreMapper.updateById(drugInnerStore);
            return true;

        }
        return false;
    }

    @Override
    public PageInfo<MarketingDrugListResp> getPrecinctDrug(DrugPageReq drugPageReq) {

        PageInfo<MarketingDrugListResp> marketingDrugListRespPageInfo = PageHelper.startPage(drugPageReq.getPageNum(), drugPageReq.getPageSize())
                .doSelectPageInfo(() -> drugMapper.getPrecinctDrug(drugPageReq));
        return marketingDrugListRespPageInfo;

    }

    @Override
    public Boolean addDrug(List<DrugImportExcelResp> successList) {
        //该导入新增 只新增药品 drug 和分类

        if (ObjectUtil.isEmpty(successList)) {
            return false;
        }
        for (DrugImportExcelResp drugResp : successList) {
            Drug selectOne = drugMapper.selectOne(new LambdaQueryWrapper<Drug>().eq(Drug::getBarCode, drugResp.getBarCode()).eq(Drug::getDisable, false));
            if (ObjectUtil.isNotEmpty(selectOne)) {
                continue;
            }

            Drug drug = new Drug();
            drug.setDrugCode(drugResp.getDrugCode());
            drug.setBarCode(drugResp.getBarCode());
            drug.setDrugName(drugResp.getDrugName());
            drug.setSpecifications(drugResp.getSpecifications());
            drug.setDosageForm(drugResp.getDosageForm());
            drug.setManufacturer(drugResp.getManufacturer());
            drug.setApprovalNumber(drugResp.getApprovalNumber());
            drug.setPrescriptionDrug(Boolean.parseBoolean(drugResp.getPrescriptionDrug()));
            drug.setDrugGenericNames(drugResp.getDrugGenericNames());
            if (ObjectUtil.isEmpty(drugResp.getCostPrice())) {
                drug.setCostPrice(BigDecimal.ZERO);
            } else {
                drug.setCostPrice(new BigDecimal(drugResp.getCostPrice()));
            }
            drug.setUnit(drugResp.getUnit());
            drug.setUniformRetailPrice(new BigDecimal(drugResp.getUniformRetailPrice()));
            drug.setState(false);
            drug.setCreateTime(new Date());
            drug.setUpdateTime(new Date());
            drug.setDisable(false);
            drug.setClassificationName(drugResp.getStairClassifiedName());
            drug.setSonClassificationName(drugResp.getSecondLevelClassifiedName());
            drug.setStock(0);

            drugMapper.insert(drug);

            //新增成功后获得drugid
            Long id = drug.getId();
            List<Store> storeList = storeManager.selectList(null);
            for (Store store : storeList
            ) {
                DrugInnerStore drugInnerStore = new DrugInnerStore();
                drugInnerStore.setStoreId(store.getId());
                drugInnerStore.setDrugId(id);
                drugInnerStore.setStock(0);
                drugInnerStore.setUpdateTime(new Date());
                drugInnerStoreMapper.insert(drugInnerStore);
            }
            boolean addClassified = drugInnerClassifiedManagementService.addClassified(drug.getId(), Long.parseLong(drugResp.getStairClassifiedId()), Long.parseLong(drugResp.getSecondLevelClassifiedId()));
            //drugProducer.sendDrugChangeMsg(new DrugChangeMessage().setDrugId(drug.getId()).setChangeType(1).setMerchantId(Long.valueOf(drugResp.getMerchantId())));
        }

        return true;
    }

     @Override
    public PageInfo<Drug> getDrugPageByClassification(ClassificationDrugPageReq req) {
        if ("0".equals(req.getClassifiedManagementId()) || "0".equals(req.getSonClassifiedManagementId())) {

            PageInfo<Drug> page = PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> drugMapper.getUncategorizedDrugs(req.getState()));

            return page;
        } else {

            DrugPageReq drugPageReq = new DrugPageReq();
            drugPageReq.setPageNum(req.getPageNum());
            drugPageReq.setPageSize(req.getPageSize());

            if (ObjectUtil.isNotEmpty(req.getClassifiedManagementId()) && !req.getClassifiedManagementId().equals("1")) {
                drugPageReq.setClassifiedManagementId(Long.valueOf(req.getClassifiedManagementId()));
            }
            if (ObjectUtil.isNotEmpty(req.getSonClassifiedManagementId()) && !req.getSonClassifiedManagementId().equals("1")) {
                drugPageReq.setSonClassifiedManagementId(Long.valueOf(req.getSonClassifiedManagementId()));
            }
            drugPageReq.setState(req.getState());
            return this.getDrugPage(drugPageReq);
        }

    }

    private boolean checkDrug(Drug drug) {
        if (ObjectUtil.isEmpty(drug)) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getBarCode()) || drug.getBarCode().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getDrugName()) || drug.getDrugName().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getTabularChart()) || drug.getTabularChart().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getSpecifications()) || drug.getSpecifications().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getUnit()) || drug.getUnit().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getDosageForm()) || drug.getDosageForm().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getManufacturer()) || drug.getManufacturer().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getApprovalNumber()) || drug.getApprovalNumber().equals("")) {
            return false;
        }
        if (ObjectUtil.isEmpty(drug.getUniformRetailPrice()) || drug.getUniformRetailPrice().equals("")) {
            return false;
        }
        DrugInnerClassifiedManagement drugInnerClassifiedManagement = drugInnerClassifiedManagementMapper.selectOne(new LambdaQueryWrapper<DrugInnerClassifiedManagement>().eq(DrugInnerClassifiedManagement::getDrugId, drug.getId()));
        if (ObjectUtil.isEmpty(drugInnerClassifiedManagement)) {
            return false;
        } else {
            Long sonClassifiedManagementId = drugInnerClassifiedManagement.getSonClassifiedManagementId();
            ClassifiedManagement classifiedManagement = classifiedManagementMapper.selectById(sonClassifiedManagementId);
            ClassifiedManagement pclassifiedManagement = classifiedManagementMapper.selectById(drugInnerClassifiedManagement.getParentClassifiedManagementId());

            if (ObjectUtil.isEmpty(classifiedManagement) || ObjectUtil.isEmpty(pclassifiedManagement)
                    || classifiedManagement.getDisable() || pclassifiedManagement.getDisable()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 添加药品
     * @param addDrugReq 添加药品请求model
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDrug(AddDrugReq addDrugReq) {
        if (ObjectUtil.isEmpty(addDrugReq.getSecondLevelClassifiedId()) || ObjectUtil.isEmpty(addDrugReq.getStairClassifiedId())) {
            throw new ApiException(500, "分类不能为空");
        }
        if (ObjectUtil.isNotEmpty(addDrugReq.getUniformRetailPrice()) && addDrugReq.getUniformRetailPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new ApiException(500, "统一零售价必须大于0");
        }

        Drug byId = drugMapper.getById(addDrugReq.getDrugId());
        // 不存在添加
        String pattern = "^[a-z0-9A-Z]+$";
        if (!addDrugReq.getBarCode().matches(pattern)) {
            throw new ApiException(501, "条码不合规" + addDrugReq.getBarCode());
        }
        if (ObjectUtil.isEmpty(byId)) {
            if (ObjectUtil.isNotEmpty(addDrugReq.getBarCode())) {
                Drug one = this.getOne(new LambdaQueryWrapper<Drug>().eq(Drug::getBarCode, addDrugReq.getBarCode()).eq(Drug::getDisable, false));
                if (!ObjectUtil.isEmpty(one)) {
                    throw new ApiException(501, "当前药品条形码已存在：" + addDrugReq.getBarCode());
                }
            }

            Drug drug = DrugConvert.INSTANCE.addDrugreq2Drug(addDrugReq);
            drug.setDisable(false);
            drug.setState(false);
            drug.setBarCode(addDrugReq.getBarCode());
            drug.setCreateTime(new Date());
            drug.setUpdateTime(new Date());
            //列表
            if (ObjectUtil.isNotEmpty(addDrugReq.getDrugCarouselMap()) && addDrugReq.getDrugCarouselMap().size() > 0) {
                drug.setTabularChart(addDrugReq.getDrugCarouselMap().get(0));
            }

            drug.setSalesVolume(0);

            this.save(drug);

            // 药品的分类
            if (ObjectUtil.isNotEmpty(addDrugReq.getStairClassifiedId())) {
                log.info("执行" + addDrugReq);
                boolean addClassified = drugInnerClassifiedManagementService.addClassified(drug.getId(), addDrugReq.getStairClassifiedId(), addDrugReq.getSecondLevelClassifiedId());
            }
            // 药品轮播图
            if (ObjectUtil.isNotEmpty(addDrugReq.getDrugCarouselMap()) && addDrugReq.getDrugCarouselMap().size() > 0) {
                boolean addResult = drugCarouselMapService.addCarouselMap(addDrugReq.getDrugCarouselMap(), drug.getId());
            }
            // 药品详细图
            if (ObjectUtil.isNotEmpty(addDrugReq.getDrugCarouselMap()) && addDrugReq.getDrugCarouselMap().size() > 0) {
                boolean addDetails = drugDetailsService.addDetails(drug.getId(), addDrugReq.getDrugCarouselMap());
            }
            //获取当前商户下所有开启的门店
            List<Store> stores = storeManager.selectList(null);
            // 添加门店药品关联
            for (Store store : stores) {
                DrugInnerStore dis = new DrugInnerStore();
                dis.setDrugId(drug.getId());
                dis.setStoreId(store.getId());
                dis.setStock(0);
                dis.setUpdateTime(new Date());
                drugInnerStoreMapper.insert(dis);
            }
            Drug drug1 = drugMapper.selectById(drug.getId());
            //发送药品信息修改消息
            //drugProducer.sendDrugChangeMsg(new DrugChangeMessage().setDrugId(drug.getId()).setChangeType(1).setMerchantId(drug1.getMerchantId()));
            return true;
        } else {

            // 更新分类
            boolean updateClassified = drugInnerClassifiedManagementService.updateClassified(addDrugReq.getDrugId(), addDrugReq.getStairClassifiedId(), addDrugReq.getSecondLevelClassifiedId());
            // 更新轮播图
            boolean carouselResult = drugCarouselMapService.updateCarouselMap(addDrugReq.getDrugCarouselMap(), addDrugReq.getDrugId());
            // 更新详细图
            boolean updateDetails = drugDetailsService.updateDetails(addDrugReq.getDrugId(), addDrugReq.getDrugCarouselMap());

            Drug drug = DrugConvert.INSTANCE.addDrugreq2Drug(addDrugReq);
            if (!drug.getBarCode().equals(byId.getBarCode())) {
                Drug one = this.getOne(new LambdaQueryWrapper<Drug>().eq(Drug::getBarCode, addDrugReq.getBarCode()).eq(Drug::getDisable, false));
                if (!ObjectUtil.isEmpty(one)) {
                    throw new ApiException(501, "当前药品条形码已存在：" + addDrugReq.getBarCode());
                }
            }
            //列表图
            if (ObjectUtil.isNotEmpty(addDrugReq.getStairClassifiedId())) {
                drug.setTabularChart(addDrugReq.getDrugCarouselMap().get(0));
            }
            drug.setUpdateTime(new Date());
            this.updateById(drug);
            //发送药品信息修改消息
           // drugProducer.sendDrugChangeMsg(new DrugChangeMessage().setDrugId(drug.getId()).setChangeType(2).setMerchantId(byId.getMerchantId()));

            return true;
        }

    }

    /**
     * 药品列表
     *
     * @param drugPageReq 药品请求model
     * @return 药品结果
     */
    @Override
    public PageInfo<Drug> getDrugPage(DrugPageReq drugPageReq) {

        if (ObjectUtil.isNotEmpty(drugPageReq.getDrugCode())) {
            String code = drugPageReq.getDrugCode();
            String s1 = code.replaceAll(" ", "").replaceAll("\t", "");
            drugPageReq.setDrugCodeList(Arrays.asList(s1.split("/")));
        }
        if (ObjectUtil.isNotEmpty(drugPageReq.getBarCode())) {
            String code = drugPageReq.getBarCode();
            String s1 = code.replaceAll(" ", "").replaceAll("\t", "");
            drugPageReq.setBarCodeList(Arrays.asList(s1.split("/")));
        }

        PageInfo<Drug> drugPageInfo = PageHelper.startPage(drugPageReq.getPageNum(), drugPageReq.getPageSize())
                .doSelectPageInfo(() -> drugMapper.getDrugPage(drugPageReq));
//        List<Drug> list = drugPageInfo.getList();
//        for (Drug drug : list) {
//            drug.setIsEntrepotDrug(Boolean.FALSE);
//            OriginalDrug originalDrug = originalDrugMapper.selectOne(new LambdaQueryWrapper<OriginalDrug>().eq(OriginalDrug::getBarCode, drug.getBarCode()));
//            if (ObjectUtil.isNotNull(originalDrug)) {
//                drug.setIsEntrepotDrug(Boolean.TRUE);
//            }
//        }
        return drugPageInfo;
    }
    /**
     * 根据id 删除
     * @param id 删除的id
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDrug(List<Long> id) {
        List<Drug> drugs = drugMapper.getIds(id);
        if (!ObjectUtil.isEmpty(drugs)) {
            for (Drug drug : drugs) {
                if (drug.getState()) {
                    throw new ApiException(500, drug.getDrugName() + "   已上架不能删除");
                }
                //判断药品是否正在参加活动 药品参加了活动 并且活动未结束
                MarketingActivityTable marketing = marketingActivityTableMapper.selectOne(new LambdaQueryWrapper<MarketingActivityTable>()
                        .eq(MarketingActivityTable::getDrugId, drug.getId())
                        .eq(MarketingActivityTable::getActivitystatusEnd, true)
                );
                //判断药品是否参加积分商城
                Result<Boolean> pointsReward = marketingFeigh.getPointsReward(drug.getId().toString(), "1", drug.getMerchantId().toString());
                if (pointsReward.getData()) {
                    throw new ApiException(500, "该药品正在参加积分商品，请先移除后再下架");
                }
                //正在参加活动
                if (ObjectUtil.isNotEmpty(marketing)) {
                    throw new ApiException(500, "不能对参加活动的药品进行删除");
                }
                //判断是否参加团购
                List<GroupBookingInnerDrug> groupBookingInnerDrugList = groupBookingInnerDrugMapper.getGroupBookingInnerDrug(drug.getId());

                if (ObjectUtil.isNotEmpty(groupBookingInnerDrugList)) {
                    throw new ApiException(500, "不能对参加团购的药品进行删除");
                }
                drug.setDisable(true);
                if (drugMapper.updateById(drug) > 0) {
                    //发送药品信息修改消息
                    //drugProducer.sendDrugChangeMsg(new DrugChangeMessage().setDrugId(drug.getId()).setChangeType(3).setMerchantId(drug.getMerchantId()));
                }
            }
            return true;
        }
        return false;
    }
     /**
     * 根据id 获得药品详细(非活动)
     *
     * @param req
     * @return 药品详细
     */
    @Override
    public DrugResp getOneDrug(DrugInfoReq req) {
        DrugResp addDrugReq = new DrugResp();
        if (ObjectUtil.isNotEmpty(req.getStoreId())) {
            // 门店查询药品基本参数
            addDrugReq = drugMapper.getOneDrug(req);
        } else {
            // 总店查询药品基本参数
            addDrugReq = drugMapper.getOneDrugPar(req);
        }
        //判断是否仓库药品
        addDrugReq.setIsEntrepotDrug(Boolean.FALSE);
//        OriginalDrug originalDrug = originalDrugMapper.selectOne(new LambdaQueryWrapper<OriginalDrug>().eq(OriginalDrug::getBarCode, addDrugReq.getBarCode()));
//        if (ObjectUtil.isNotNull(originalDrug)) {
//            addDrugReq.setIsEntrepotDrug(Boolean.TRUE);
//        }

        // 获得分类
        ClassResp classified = classifiedManagementService.getClassifiedBigAndSmall(req.getDrugId());
        //轮播图
        Map<Integer, String> drugCarouselMap = drugCarouselMapService.getCarouselMap(req.getDrugId());
        // 获取详细图
        Map<Integer, String> details = drugDetailsService.getDetails(req.getDrugId());
        if (!ObjectUtil.isEmpty(classified)) {
            addDrugReq.setStairClassifiedName(classified.getClassName());
            addDrugReq.setStairClassifiedId(classified.getClassId());
            if (!ObjectUtil.isEmpty(classified.getSonClassName())) {
                addDrugReq.setSecondLevelClassifiedName(classified.getSonClassName());
                addDrugReq.setSecondLevelClassifiedId(classified.getSonClassId());
            }
        }
        if (!ObjectUtil.isEmpty(drugCarouselMap)) {
            addDrugReq.setDrugCarouselMap(drugCarouselMap);
        }
        if (!ObjectUtil.isEmpty(details)) {
            addDrugReq.setDetailedImage(details);
        }
        return addDrugReq;
    }
    @Override
    public boolean updateBatchState(EditClassifyDto dto) {
        List<Long> drugIds = dto.getDrugId();
        for (Long drugId : drugIds) {
            //药品参加了活动 并且活动未结束
            MarketingActivityTable marketing = marketingActivityTableMapper.selectOne(new LambdaQueryWrapper<MarketingActivityTable>()
                    .eq(MarketingActivityTable::getDrugId, drugId)
                    .eq(MarketingActivityTable::getActivitystatusEnd, true).last("limit 1")
            );
            //正在参加活动
            if (ObjectUtil.isNotEmpty(marketing)) {
                throw new ApiException(500, "不能对参加活动的药品进行修改");
            }

            //药品基本信息不完整 则不能上下架
            Drug d = drugMapper.selectOne(new LambdaQueryWrapper<Drug>().eq(Drug::getId, drugId));
            if (!checkDrug(d)) {
                throw new ApiException(500, "药品基本信息不完整，请先编辑录入完整的药品基本信息");
            }
            //判断药品是否参与积分商城
            Result<Boolean> pointsReward = marketingFeigh.getPointsReward(d.getId().toString(), "1", d.getMerchantId().toString());
            if (pointsReward.getData()) {
                throw new ApiException(500, "该药品正在参加积分商品，请先移除后再下架");
            }

            this.update(new LambdaUpdateWrapper<Drug>()
                    .set(!ObjectUtil.isEmpty(dto.getState()), Drug::getState, dto.getState())
                    .eq(Drug::getId, drugId)
            );
            Drug drug = drugMapper.selectById(drugId);
//            drugProducer.sendDrugChangeMsg(new DrugChangeMessage().
//                    setDrugId(drugId).
//                    setChangeType(4).
//                    setState(dto.getState()).setMerchantId(drug.getMerchantId()));
        }
        return true;
    }
    @Override
    public boolean updateClassify(EditClassifyDto dto) {
        List<Long> drugIds = dto.getDrugId();

        if (ObjectUtil.isEmpty(dto.getStairClassifiedId()) || ObjectUtil.isEmpty(dto
                .getSecondLevelClassifiedId())) {
            return false;
        }
        for (Long drugId : drugIds) {
            drugInnerClassifiedManagementService.updateClassified(drugId, dto.getStairClassifiedId(), dto.getSecondLevelClassifiedId());
        }
        return true;
    }
    @Override
    public ImportDrugResp batchEditDrugDetail(MultipartFile file) {
//        ExcelBatchEditDrugListener excelBatchEditDrugListener = new ExcelBatchEditDrugListener(classifiedManagementService, drugService, drugProducer);
//        try {
//            EasyExcel.read(file.getInputStream(), BatchEditDrugReq.class, excelBatchEditDrugListener).sheet().headRowNumber(2).doRead();
//            List<BatchEditDrugReq> successList = excelBatchEditDrugListener.getSuccessList();
//            List<BatchEditDrugReq> failedList = excelBatchEditDrugListener.getFailedList();
//            ImportDrugResp resp = new ImportDrugResp();
//            resp.setFailTotal(Long.valueOf(failedList.size()));
//            resp.setSuccessTotal(Long.valueOf(successList.size()));
//            resp.setTotleData(excelBatchEditDrugListener.getBatchEditDrugReqList().size());
//            //存储异常结果
//            if (ObjectUtil.isNotEmpty(failedList)) {
//                StoreImportRecord storeImportRecord = new StoreImportRecord();
//                storeImportRecord.setRecord(JSONUtil.toJsonStr(failedList));
//                storeImportRecord.setCreateTime(new Date());
//                storeImportRecordMapper.insert(storeImportRecord);
//                resp.setStoreImportRecordId(storeImportRecord.getStoreImportRecordId());
//            }
//            return resp;
//        } catch (IOException e) {
//            throw new ApiException(500, "数据处理失败");
//        }
//
        return null;
    }
}
